import SDKManager from "../SDKManager";
import { ResultState } from "../SDKConfig";
import BagManager from "../../game/bag/BagManager";
import { InitValue } from "../../public/Config";

const { ccclass, property, requireComponent } = cc._decorator;

@ccclass
@requireComponent(cc.Button)
export default class ShareComp extends cc.Component {

    @property
    adSite: number = 0;

    // @property({
    //     type: cc.Component.EventHandler,
    //     displayName: "回调函数"
    // })
    // callback = new cc.Component.EventHandler();

    start() {
        let hasShare = SDKManager.getChannel().hasShare();
        if (hasShare) {
            this.node.on(cc.Node.EventType.TOUCH_END, this.onButtonClick, this)
        } else {
            this.node.active = hasShare
        }

    }

    onButtonClick() {
        SDKManager.getChannel().showShare(this.adSite, (result: ResultState) => {

            if (result == ResultState.YES) {
                if (cc.isValid(this.node)) {
                    BagManager.addCoin(InitValue.shareNum, this.node.parent.convertToWorldSpaceAR(this.node.getPosition()))
                } else {
                    BagManager.addCoin(InitValue.shareNum)
                }


            }
        });
    }

}
